package com.a214.yxz.yuejian013.Model;
import com.a214.yxz.yuejian013.Interface.PeiXunFace;
import com.a214.yxz.yuejian013.JavaBean.PeiXunDetail;
import com.a214.yxz.yuejian013.JavaBean.PeiXunJiBie;
import com.a214.yxz.yuejian013.JavaBean.PeiXunListBean;
import com.a214.yxz.yuejian013.Listener.JieBieListener;
import com.a214.yxz.yuejian013.Listener.PeiXunDetailListener;
import com.a214.yxz.yuejian013.Listener.PeiXunListListener;
import com.a214.yxz.yuejian013.Service.PxService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class PeiXunModel implements PeiXunFace {

    private Retrofit retrofit;
    private PxService pxService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址
    public PeiXunModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getList(String SessionId, int page,final PeiXunListListener listListener) {
                pxService = retrofit.create(PxService.class);
        Call<List<PeiXunListBean>> call =pxService.getPeiXunList(SessionId,page);
        call.enqueue(new Callback<List<PeiXunListBean>>() {
            @Override
            public void onResponse(Call<List<PeiXunListBean>> call, Response<List<PeiXunListBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<PeiXunListBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }



    @Override
    public void getPeiXunDetailList(String sessionId, String id,final PeiXunDetailListener listener) {
        pxService = retrofit.create(PxService.class);
        Call<PeiXunDetail> call= pxService.getPeiXunDetail(sessionId,id);
        call.enqueue(new Callback<PeiXunDetail>() {
            @Override
            public void onResponse(Call<PeiXunDetail> call, Response<PeiXunDetail> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<PeiXunDetail> call, Throwable t) {

            }
        });
    }

    @Override
    public void getJieBie(String SessionId, int page, String mod, int degree, final JieBieListener listener) {
        pxService = retrofit.create(PxService.class);
        Call<List<PeiXunJiBie>> call=pxService.peixunList(SessionId,page,mod,degree);
        call.enqueue(new Callback<List<PeiXunJiBie>>() {
            @Override
            public void onResponse(Call<List<PeiXunJiBie>> call, Response<List<PeiXunJiBie>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponseRumen(response.body());
                } else {
                    listener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<PeiXunJiBie>> call, Throwable t) {

            }
        });
    }



}



